package com.ebelter.btcomlib.models.bluetooth.products.hrscale;


import android.content.Context;
import android.text.TextUtils;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.base.BaseManager;
import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IDataReadWriteCallback;

import com.ebelter.btcomlib.models.bluetooth.products.hrscale.interfaces.HRIScaleMeasureCallback;

import com.ebelter.btcomlib.models.bluetooth.products.scale.ScaleOtaUpdate;

import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.Arrays;


/**
 * 体脂秤
 */
public class HRScaleManager extends BaseManager{

    private static final String TAG = "HRScaleManager";




    private HRScaleBytesMakeFatory mHRScaleBytesMakeFatory;//制包工具类

    private HRScaleBytesAnalysisFatory mHRScaleBytesAnalysisFatory;//解包工具类

    private ScaleOtaUpdate mScaleOtaUpdate;//ota的升级工具类

    public HRScaleManager(Context mContext) {
        super(mContext, ProductStyle.HRSCALE,HrScaleConstant.HR_SCALE_SERVICE_UUID,
                new String[]{HrScaleConstant.HR_SCALE_WRITE_UUID},new String[]{HrScaleConstant.HR_SCALE_READ_UUID},true,300);


        mHRScaleBytesMakeFatory=new HRScaleBytesMakeFatory();

        mHRScaleBytesAnalysisFatory=new HRScaleBytesAnalysisFatory();

        mServiceGatt.setIDataReadWriteCallback(new IDataReadWriteCallback() {
            @Override
            public void onCharacteristicChanged(byte[] data) {
                LogUtils.i(TAG,"ScaleBlue--IDataReadWriteCallback-onCharacteristicChanged--读到-"+ Arrays.toString(data));
                if (mHRScaleBytesAnalysisFatory!=null)mHRScaleBytesAnalysisFatory.resultAnalysis(data);
            }

            @Override
            public void writeData(byte[] data) {
                // LogUtils.i(TAG,"ScaleBlue--IDataReadWriteCallback-writeData--写入-"+ Arrays.toString(data));
            }
        });
    }







    //设置测量状态监听
    public void setIMeasureResultCallback(HRIScaleMeasureCallback mHRIScaleMeasureCallback) {
        mHRScaleBytesAnalysisFatory.setmIMeasureResultCallback(mHRIScaleMeasureCallback);
    }






    //下发用户信息 下发设置成功后会在在IMeasureResultCallback中setUserInfoSuccess()回调回来
    public void sendUserInfo(ScaleUser scaleUser){
        if (scaleUser==null)return;
        addSendMsg(mHRScaleBytesMakeFatory.getUserInfoSmg(scaleUser));
    }


    //发送心跳包
    public void sendHeartCheck(){
        addSendMsg(mHRScaleBytesMakeFatory.getHEARTSmg());
    }


    public void getVersion(){
        addSendMsg(mHRScaleBytesMakeFatory.getVersion());
    }





    public void sendMesureDataACK() {
        addSendMsg(mHRScaleBytesMakeFatory.getMesureDataACK());
    }



    /**
     * 下发同步系统时间命令
     */
    public void syncSystemClock() {
        LogUtils.i(TAG, "----syncSystemClock-----send sync system time cmd.");
        addSendMsg(mHRScaleBytesMakeFatory.getSyncSystemClock());
    }

    public void deleteAllUserInfo() {
        addSendMsg(mHRScaleBytesMakeFatory.deleteAllUserInfo());
    }








    public boolean isBondDevice(){
        String add=getBondDeviceAddress();
        return !TextUtils.isEmpty(add);
    }

    public String getBondDeviceAddress(){
        return SpUtil.readString(HrScaleConstant.HR_SCALE_BOND_DEVICE_ADDRESS,null);
    }

    public void setBondDeviceAddress(String address){
        SpUtil.writeString(HrScaleConstant.HR_SCALE_BOND_DEVICE_ADDRESS,address);
    }


    //kg转磅
    public static float kg2lb(float kg){
        return (float) (kg * 144480.0/65536.0);
    }
    //磅转kg
    public static float lb2kg(float lb){
        return (float) (lb * 65536.0/144480.0);
    }




    public static String getBMIDesc(float bmi){
        bmi= NumUtils.numBaoLiuWei1(bmi);
        if (bmi<18.5f){
            return BaseActivity.getString_(R.string.pianqing);
        }else if (bmi>=18.5f&&bmi<=23.9f){
            return BaseActivity.getString_(R.string.biaozhun);
        }else if (bmi>=24.0f&&bmi<=27.9f){
            return BaseActivity.getString_(R.string.pianzhong);
        }else {
            return BaseActivity.getString_(R.string.chaozhong);
        }
    }

    public static int getBMILevel(float bmi){
        bmi= NumUtils.numBaoLiuWei1(bmi);
        if (bmi<18.5f){
            return 0;
        }else if (bmi>=18.5f&&bmi<=23.9f){
            return 1;
        }else if (bmi>=24.0f&&bmi<=27.9f){
            return 2;
        }else {
            return 3;
        }
    }

}
